﻿component{
	
	include "../../inc/lib.cfm"; 
	public any function getTree(){
		var result = {};
		var hql = "from resource where resourceid is null order by resourceid asc,seq asc";
		var result = ormExecuteQuery(hql,false);
		
		for(i = 1; i <= arraylen(result); i++)
		{
			result[i]['state'] = result[i].hasChildren() ? 'closed' : 'open';
		}
		
		return result;
	}
	
	
	public any function checkUserMenuTree(){
		var idList="";
		for(var i =1; i<= arraylen(session.auth.allRsAuths);i++){
			if(session.auth.allRsAuths[i].resourcetypeid == 1){
				idList= listAppend(idList,session.auth.allRsAuths[i].resourceid);
			}
		}
		return listremoveduplicates(idList);
	}
	
	public any function getFlatTree(){
		var idList = checkUserMenuTree();
		
		var result = {};
		var hql = "from resourceflat where resourcetypeid = 1 and id in (#idList#)  order by resourceid asc,seq asc";
		var result = ormExecuteQuery(hql,false);
		var newResult = [];
		for(var i = 1;i<= arraylen(result);i++){
			newResult[i]['id'] = result[i].getId();
			newResult[i]['name'] = result[i].getText();
			newResult[i]['url'] = result[i].getUrl();
			if( isNull (result[i].getResourceId())){
				newResult[i]['_parentid'] = 0;
			}else{
				newResult[i]['_parentid'] = result[i].getResourceId();
			}
		}
		
		return newResult;
	}
	
	//组织机构树对象
	public any function getOrgGrantTree(numeric id){
		//从组织资源对应对象中获取传递过来的指定ID的数据内容
		var hql = "from organizationresource where organizationid =" & arguments.id;
		var result = ormExecuteQuery(hql,false);
		
		//将资源ID组成idList字符串内容
		idList="";
		for(var i = 1;i<= arraylen(result);i++){
			idList = listAppend(idList,result[i].getResourceid().getId());
		}
		
		//只获取idList的节点内容
		var checkResource = [];
		for(var i=1;i<=listlen(idList);i++){
			checkResource[i] = getResourceById( listgetat(idList,i) );			
		}

		return checkResource;

	}
	
	
	public any function	getResourceById(numeric id){
		result = entityLoadByPk("resource", arguments.id); 
		return result;	
	}
	
	//用户角色树对象
	public any function getRoleGrantTree(numeric id){
		//从组织资源对应对象中获取传递过来的指定ID的数据内容
		var hql = "from roleresource where roleid =" & arguments.id;
		var result = ormExecuteQuery(hql,false);
		
		//将资源ID组成idList字符串内容
		idList="";
		for(var i = 1;i<= arraylen(result);i++){
			idList = listAppend(idList,result[i].getResourceid().getId());
		}
		
		//只获取idList的节点内容
		var checkResource = [];
		for(var i=1;i<=listlen(idList);i++){
			checkResource[i] = getResourceById( listgetat(idList,i) );			
		}

		return checkResource;	
	}
	
	
	function recuseTreeCheck(any o) {
	  	if( structkeyexists(o,"getChildren") && arraylen(o.getChildren())>=0) {
			//特别注意变量i需要在循环的时候重新初始值，不然会错乱
			for(var i=1;i<=arraylen(o.getChildren());i++){
				child = o.getChildren()[i];
				//如果获取的ID值在idList中有存在，那么将它的checked设置为true
				if(listfindnocase(idlist,child.getId())){
					child["checked"] = true;
				}
				if(structkeyexists(child,"getChildren") && arraylen(child.getChildren())>=0) {
		          //递归调用
		          recuseTreeCheck(child);
			    }
			}
		}
	}
	
	public any function list(rc){
		
		var result = {};
		var hql = " from resourceflat order by resourceid asc,seq asc";
		var result["rows"] = ormExecuteQuery(hql,false);
		var totalhql = "select count(id) as total from resourceflat";
		var result["total"] = ormExecuteQuery(totalhql,true);
		
		newResult = [];
		
		for(var i=1;i<=arraylen(result.rows);i++){
			var tmpResult = {};
			
			tmpResult["url"] = result.rows[i].getUrl();
			tmpResult["text"] = result.rows[i].getText();
			tmpResult["id"] = result.rows[i].getId();
			tmpResult["description"] = result.rows[i].getDescription();
			tmpResult["state"] = result.rows[i].getState();
			tmpResult["seq"] = result.rows[i].getSeq();
			tmpResult["_parentId"] = result.rows[i].getResourceid();
			tmpResult["resourcetype"] = getResourceTypeById(result.rows[i].getResourcetypeid()).getName();
			
			var checkChildHql = "select count(id) as total from resourceflat where resourceid = #result.rows[i].getID()#";
			var ctotal = ormExecuteQuery(checkChildHql,true);
			if(ctotal>0){
				tmpResult['state'] ='closed';
			}else{
				 tmpResult['state'] = 'open';
			}
			
			newResult[i] = tmpResult;
		}
		result["rows"] = newResult;
		
		return result;
	}
	
	//通过id获取resourceType对象
	public any function	getResourceTypeById(numeric id){
		result = entityLoadByPk("resourcetype", arguments.id); 
		return result;	
	}
	
	//获取resourcetype对象
	public any function getResourcetype(rc){
		var result = {};
		var hql = " from resourcetype ";
		var result = ormExecuteQuery(hql,false);
		return result;
	}
	
	//获取操作
	public any function get(numeric id){
		result = entityLoadByPk("resourceflat", arguments.id);
		return result;
	}

	//新增操作，调用的是resourceflat实体对象	
	public any function save(any rc){
		transaction {
            var q = entityNew("resourceflat");
			include "../../inc/saveandupdate.cfm";
        }
        return q;
	}
	
	//删除操作
	//organizationresource表中的resourceid和organizationid都建立了cascade的delete约束，所以删除主表时，子表数据自动删除
	public any function delete(numeric id){
		transaction {
            var resource = entityLoadByPk("resource", arguments.id);
            //设置全局ids的变量，ids不可变，因为递归函数中用ids
            ids ="";
            //将自身的id追加到idList这个列表中去
            idList = listprepend(RecursiveChild(resource),arguments.id); 

            for(var i=1;i<=listlen(idList);i++){
            	var resource = entityLoadByPk("resource", listgetat(idList,i));
            	entityDelete(resource);
            }
         	
            transactionCommit();
        }
	}
	
	//修改操作
	public any function update(any rc){
		transaction {
            var q = entityLoadByPk("resourceflat", rc.id);
			include "../../inc/saveandupdate.cfm";
        }
        return q;
	}
	
	
	public any function	getPermissionById(numeric id){
		result = entityLoadByPk("permission", arguments.id); 
		return result;	
	}
	
}